import MMUI.Checkbox;
import MMUI.Container;
import MMUI.MMUIFactory;
import MMUI.Radio;
import MMUI.Widget;
import com.google.common.base.Objects;
import fr.esir.imse.PollSystemStandaloneSetup;
import fr.esir.imse.mapping.MappingDSLStandaloneSetup;
import fr.esir.imse.mapping.mappingDSL.Mapping;
import fr.esir.imse.pollSystem.Option;
import fr.esir.imse.pollSystem.Poll;
import fr.esir.imse.pollSystem.PollSystem;
import fr.esir.imse.pollSystem.Question;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.Resource.Factory.Registry;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.IterableExtensions;

@SuppressWarnings("all")
public class Transformateur {
  public void transformateur(final String pollSystemDSL, final String mapping) {
    Container ui = MMUIFactory.eINSTANCE.createContainer();
    ResourceSetImpl _resourceSetImpl = new ResourceSetImpl();
    ResourceSet resourceSet = _resourceSetImpl;
    Mapping map = this.testParsingMapping(mapping);
    PollSystem poll = this.testParsingPollSystem(pollSystemDSL);
    EList<Poll> polls = poll.getPolls();
    Iterator<Poll> pollsIterator = polls.iterator();
    boolean _hasNext = pollsIterator.hasNext();
    boolean _while = _hasNext;
    while (_while) {
      {
        Poll p = pollsIterator.next();
        EList<Question> questionsPoll = p.getQuestions();
        Iterator<Question> questionsPollIterator = questionsPoll.iterator();
        boolean _hasNext_1 = questionsPollIterator.hasNext();
        boolean _while_1 = _hasNext_1;
        while (_while_1) {
          {
            Question _next = questionsPollIterator.next();
            Question questionPoll = ((Question) _next);
            EList<fr.esir.imse.mapping.mappingDSL.Question> questionsMap = map.getQuestions();
            fr.esir.imse.mapping.mappingDSL.Question questionMap = null;
            Iterator<fr.esir.imse.mapping.mappingDSL.Question> questionsMapIterator = questionsMap.iterator();
            boolean _and = false;
            boolean _hasNext_2 = questionsMapIterator.hasNext();
            if (!_hasNext_2) {
              _and = false;
            } else {
              boolean _equals = Objects.equal(questionMap, null);
              _and = (_hasNext_2 && _equals);
            }
            boolean _while_2 = _and;
            while (_while_2) {
              {
                fr.esir.imse.mapping.mappingDSL.Question element = questionsMapIterator.next();
                String _id = element.getId();
                String _id_1 = questionPoll.getId();
                boolean _equals_1 = _id.equals(_id_1);
                if (_equals_1) {
                  questionMap = element;
                }
              }
              boolean _and_1 = false;
              boolean _hasNext_3 = questionsMapIterator.hasNext();
              if (!_hasNext_3) {
                _and_1 = false;
              } else {
                boolean _equals_1 = Objects.equal(questionMap, null);
                _and_1 = (_hasNext_3 && _equals_1);
              }
              _while_2 = _and_1;
            }
            Widget widget = null;
            String _widget = questionMap.getWidget();
            boolean _equals_1 = Objects.equal(_widget, "radiobutton");
            if (_equals_1) {
              Radio _createRadio = MMUIFactory.eINSTANCE.createRadio();
              widget = _createRadio;
            } else {
              Checkbox _createCheckbox = MMUIFactory.eINSTANCE.createCheckbox();
              widget = _createCheckbox;
            }
            String _text = questionPoll.getText();
            widget.setText(_text);
            EList<Option> option = questionPoll.getOptions();
            Iterator<Option> optionIterator = option.iterator();
            boolean _hasNext_3 = optionIterator.hasNext();
            boolean _while_3 = _hasNext_3;
            while (_while_3) {
              {
                MMUI.Option opt = MMUIFactory.eINSTANCE.createOption();
                Option _next_1 = optionIterator.next();
                String _text_1 = _next_1.getText();
                opt.setText(_text_1);
                EList<MMUI.Option> _options = widget.getOptions();
                _options.add(opt);
              }
              boolean _hasNext_4 = optionIterator.hasNext();
              _while_3 = _hasNext_4;
            }
            EList<Widget> _widget_1 = ui.getWidget();
            _widget_1.add(widget);
          }
          boolean _hasNext_2 = questionsPollIterator.hasNext();
          _while_1 = _hasNext_2;
        }
      }
      boolean _hasNext_1 = pollsIterator.hasNext();
      _while = _hasNext_1;
    }
    Registry _resourceFactoryRegistry = resourceSet.getResourceFactoryRegistry();
    Map<String,Object> _extensionToFactoryMap = _resourceFactoryRegistry.getExtensionToFactoryMap();
    XMIResourceFactoryImpl _xMIResourceFactoryImpl = new XMIResourceFactoryImpl();
    _extensionToFactoryMap.put(
      Registry.DEFAULT_EXTENSION, _xMIResourceFactoryImpl);
    File _file = new File("myUI.xmi");
    String _absolutePath = _file.getAbsolutePath();
    URI fileURI = URI.createFileURI(_absolutePath);
    Resource resource = resourceSet.createResource(fileURI);
    EList<EObject> _contents = resource.getContents();
    _contents.add(ui);
    EList<EObject> _contents_1 = resource.getContents();
    Container _createContainer = MMUIFactory.eINSTANCE.createContainer();
    _contents_1.add(_createContainer);
    try {
      resource.save(Collections.EMPTY_MAP);
    } catch (final Throwable _t) {
      if (_t instanceof IOException) {
        final IOException e = (IOException)_t;
      } else {
        throw Exceptions.sneakyThrow(_t);
      }
    }
    Resource loadedModel = resourceSet.getResource(fileURI, true);
    EList<EObject> _contents_2 = loadedModel.getContents();
    EObject _head = IterableExtensions.<EObject>head(_contents_2);
    java.awt.Container loadedContainer = ((java.awt.Container) _head);
  }
  
  public PollSystem testParsingPollSystem(final String pollSystem) {
    ResourceSetImpl _resourceSetImpl = new ResourceSetImpl();
    ResourceSet resourceSet = _resourceSetImpl;
    PollSystemStandaloneSetup.doSetup();
    URI _createURI = URI.createURI(pollSystem);
    Resource resource = resourceSet.getResource(_createURI, true);
    EList<EObject> _contents = resource.getContents();
    EObject _head = IterableExtensions.<EObject>head(_contents);
    return ((PollSystem) _head);
  }
  
  public Mapping testParsingMapping(final String mapping) {
    ResourceSetImpl _resourceSetImpl = new ResourceSetImpl();
    ResourceSet resourceSet = _resourceSetImpl;
    MappingDSLStandaloneSetup.doSetup();
    URI _createURI = URI.createURI(mapping);
    Resource resource = resourceSet.getResource(_createURI, true);
    EList<EObject> _contents = resource.getContents();
    EObject _head = IterableExtensions.<EObject>head(_contents);
    return ((Mapping) _head);
  }
}
